package leetcode._203;

public class Sum {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		System.out.println(sum(arr));
	}

	public static int sum(int[] arr) {
//		return sum0(arr, 0);
//		return sum1(arr, arr.length - 1);
		return sum2(0, 0, arr);
	}

	private static int sum0(int[] arr, int leftIdex) {
		return leftIdex == arr.length - 1 ? arr[arr.length - 1] : arr[leftIdex] + sum0(arr, leftIdex + 1);
	}

	private static int sum1(int[] arr, int rightIndex) {
		return rightIndex == 0 ? arr[0] : arr[rightIndex] + sum1(arr, rightIndex - 1);
	}

	private static int sum2(int total, int leftIndex, int[] arr) {
		return leftIndex == arr.length ? total : sum2(arr[leftIndex] + total, leftIndex + 1, arr);
	}

}
